<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="form" :model="form" label-width="80px">
      <el-row>
        <el-col :span="5" :offset="2">
          <el-form-item label="小说名称" prop="novelName">
            <el-input v-model="form.novelName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="2">
          <el-form-item label="收费类型" prop="chargeType">
            <el-radio-group v-model="form.chargeType">
              <el-radio label="0">免费看</el-radio>
              <el-radio label="1">VIP可看</el-radio>
              <el-radio label="2">钻石观看</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="2">
          <el-form-item label="消耗钻石" prop="consumeDiamond" v-if="form.chargeType == '2'">
            <el-input v-model="form.consumeDiamond" placeholder="请输入消耗钻石" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">章节信息</h4>
    <el-table v-loading="loading" :row-key="getRowKey" @row-click="clickRow" ref="table" @selection-change="handleSelectionChange" :data="chapters.slice((pageNum-1)*pageSize,pageNum*pageSize)">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
      <el-table-column label="唯一标识" align="center" prop="id" />
      <el-table-column label="小说名称" align="center" prop="novelName" />
      <el-table-column label="章节标题" align="center" prop="chapterTitle" />
      <el-table-column label="收费类型" align="center" prop="chargeType" >
        <template slot-scope="scope">
          {{chargeTypeOptions[scope.row.chargeType]['label']}}
        </template>
      </el-table-column>
      <el-table-column label="消耗钻石" align="center" prop="consumeDiamond" />
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />

    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {getBatchChapter, updateBatchChapter} from "@/api/pixiv/novelChapter";

export default {
  name: "Batch",
  data() {
    return {
       // 遮罩层
      loading: true,
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 10,
      novelId: "",
      // 选中角色编号
      chapterIds:[],
      // 角色信息
      chapters: [],
      // 用户信息
      form: {},
      chargeTypeOptions: [{
        "label": "免费看",
        "value": "0"
      }, {
        "label": "VIP可看",
        "value": "1"
      }, {
        "label": "钻石观看",
        "value": "2"
      }]
    };
  },
  created() {
    this.novelId = this.$route.params && this.$route.params.novelId;
    if (this.novelId) {
      this.loading = true;
      getBatchChapter(this.novelId).then((response) => {
        this.form = response.data.novelInfo;
        this.chapters = response.data.chapters;
        this.total = this.chapters.length;
        this.$nextTick(() => {
          this.chapters.forEach((row) => {
            if (row.flag) {
              this.$refs.table.toggleRowSelection(row);
            }
          });
        });
        this.loading = false;
      });
    }
  },
  methods: {
    /** 单击选中行数据 */
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.chapterIds = selection.map((item) => item.id);
    },
    // 保存选中的数据编号
    getRowKey(row) {
      return row.id;
    },
    /** 提交按钮 */
    submitForm() {
      const chargeType = this.form.chargeType;
      const consumeDiamond = this.form.consumeDiamond;
      const chapterIds = this.chapterIds.join(",");
      updateBatchChapter({ chargeType: chargeType, consumeDiamond: consumeDiamond, chapterIds: chapterIds }).then((response) => {
        this.$modal.msgSuccess("批量设置成功");
        this.close();
      });
    },
    /** 关闭按钮 */
    close() {
      const obj = { path: "/pixiv/novel-chapter/index/" + this.novelId };
      this.$tab.closeOpenPage(obj);
    },
  },
};
</script>
